/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.model.vo;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;

import java.util.Objects;

/**
 * 分页表数据模型
 * @author zpy
 * @version 1.0.0
 */
public class TableDataModel<T> {
    public static final Integer LIMIT = 10;

    private ListProperty<T> body;
    private IntegerProperty pageCount;
    private IntegerProperty selectedIndex;

    public ListProperty<T> bodyProperty() {
        if(Objects.isNull(body)){
            body = new SimpleListProperty<>(FXCollections.observableArrayList());
        }
        return body;
    }

    public int getSelectedIndex() {
        return selectedIndexProperty().get();
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex.set(selectedIndex);
    }

    public T getSelectedItem(){
        if(getSelectedIndex()==-1){
            return null;
        }
        return body.get(getSelectedIndex());
    }

    public IntegerProperty selectedIndexProperty() {
        if(Objects.isNull(selectedIndex)){
            selectedIndex = new SimpleIntegerProperty();
        }
        return selectedIndex;
    }

    public int getPageCount() {
        return pageCount.get();
    }

    public IntegerProperty pageCountProperty() {
        if(Objects.isNull(pageCount)){
            pageCount = new SimpleIntegerProperty();
        }
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount.set(pageCount);
    }

}